<?php

namespace App\Admin\Actions;

use App\Models\Message;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class Deleted extends RowAction
{
    public $name = '删除';

    public function handle(Model $model, Request $request)
    {
        // 获取表单中的`reason`值
        $reason = $request->get('reason');
        $model->status = -1;
        $model->data = array_merge($model->data ?: [], ['delete_reason' => $reason]);
        $model->save();
        // 发送消息通知
        $message = new Message([
            'receive_user_id' => $model->user_id,
            'send_user_id' => 0,
            'content_id' => $model->id,
            'type' => 0,
            'title' => '删除信息',
            'descrition' => '【' . $model->title . '】被删除，原因：' . $reason,
        ]);
        $message->save();
        return $this->response()->success('Success message.')->refresh();
    }

    public function form()
    {
        $this->textarea('reason', '原因')->rules('required');
    }

}
